package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.dto.EnvironmentalDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.CompanyMapper;
import com.ant.storage.mapper.DosageMapper;
import com.ant.storage.mapper.EnvironmentalMapper;
import com.ant.storage.pojo.Dictionary;
import com.ant.storage.pojo.Dosage;
import com.ant.storage.pojo.Environmental;
import com.ant.storage.service.IDictionaryService;
import com.ant.storage.service.IEnvironmentalService;
import com.ant.storage.vo.EnvironmentalVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-23
 */
@Service
public class EnvironmentalServiceImpl extends ServiceImpl<EnvironmentalMapper, Environmental> implements IEnvironmentalService {

    @Autowired
    DosageMapper dosageMapper;

    @Autowired
    CompanyMapper companysMapper;

    @Autowired
    IDictionaryService dictionaryService;

    @Override
    public List<Environmental> listById(Integer id) {
        return baseMapper.selectList(new QueryWrapper<Environmental>().eq("product_id",id));
    }

    @Override
    public PageInfo<EnvironmentalVo> getEnvironmentalPage(EnvironmentalDto dto) {
        try {
            PageHelper.startPage(1, 10);
            QueryWrapper<Environmental> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");

            List<EnvironmentalVo> list = this.baseMapper.selectLists();
            list.stream().forEach(item->{
                List<Dosage> dosageList = dosageMapper.selectList(new QueryWrapper<Dosage>().eq("parent_id", item.getId()));
                item.setDosageList(dosageList);
            });

//            PageInfo<EnvironmentalVo> info = new PageInfo<>(this.baseMapper.selectList(wrapper));
            PageInfo<EnvironmentalVo> info = new PageInfo<>(list);
            return info;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    @Transactional
    public void save(EnvironmentalDto dto) {
        // 获取值
        String accessoryCode = dto.getAccessoryCode();
        List<Integer> resultList = Arrays.stream(accessoryCode.split(","))
                .filter(s -> !s.isEmpty()) // 过滤空字符串
                .map(Integer::parseInt)   // 转换为整数
                .collect(Collectors.toList());
        Integer lastItem = 1;
        if (!resultList.isEmpty()) { // 先判断列表非空
             lastItem = resultList.get(resultList.size() - 1);
        }
        Dictionary val = dictionaryService.getOne(new QueryWrapper<Dictionary>()
                .eq("id", lastItem)
                .eq("dic_type",3));
        Environmental tal=new Environmental();
        tal.setProductId(dto.getProductId());
        tal.setAccessoryName(dto.getAccessoryName());
        tal.setAccessoryCategory(dto.getAccessoryCategory());
        tal.setAccessoryNature(dto.getAccessoryNature());
        tal.setAccessoryKind(dto.getAccessoryKind());
        tal.setAccessoryType(dto.getAccessoryType());
        tal.setAccessoryTypeOne(dto.getAccessoryTypeOne());
        tal.setAccessoryTypeTwo(dto.getAccessoryTypeTwo());
        tal.setAccessoryCode(dto.getAccessoryCode());
        tal.setManufacturer(dto.getManufacturer());
        tal.setManufacturerTel(dto.getManufacturerTel());
        tal.setProcessSteps(dto.getProcessSteps());
        tal.setDensity(dto.getDensity());
        tal.setQualityScore(dto.getQualityScore());
        tal.setVocUrl(dto.getVocUrl());
        tal.setVocValues(dto.getVocValues());
        tal.setBenefit(dto.getBenefit());
        tal.setCreationTime(LocalDateTime.now());
        baseMapper.insert(tal);
        Integer isVoc=1;
        if( Double.valueOf(dto.getVocValues())> Integer.valueOf(val==null?"0": val.getDicValue())){
            isVoc=2;
        }
        Integer finalIsVoc = isVoc;
        dto.getDosageList().stream().forEach(item->{
            Dosage dosage=new Dosage();
            dosage.setParentId(tal.getId());
            dosage.setYears(item.getYears());
            dosage.setIsVoc(finalIsVoc);
            dosage.setVocName(item.getVocName());
            dosage.setVocDosage(item.getVocDosage());
            dosageMapper.insert(dosage);
        });
    }
}
